package com.company.modeldesign;

public class MyCircularQueue {

    int front;
    int tail;
    int size;

    int[] entiry;

    public MyCircularQueue(int k) {
        entiry = new int[k];
        front = -1;
        tail = -1;
        size = 0;
    }

    private int nextIndex(int index) {
        if (index == entiry.length - 1 || index == -1) {
            return 0;
        }
        return index + 1;
    }

    private int preIndex(int index) {
        if (index == 0) {
            return entiry.length - 1;
        }
        return index - 1;
    }

    public boolean enQueue(int value) {

        if (isFull()) {
            return false;
        }
        if (isEmpty()) {
            front = 0;
        }
        size++;
        //添加元素  由队尾添加 操作 tail 指针
        tail = nextIndex(tail);
        entiry[tail] = value;
        return true;
    }

    public boolean deQueue() {
        if (isEmpty()) {
            return false;
        }

        size--;
        front = nextIndex(front);
        if (isEmpty()) {
            front = -1;
            tail = -1;
        }
        return true;
    }

    public int Front() {
        if (isEmpty()) {
            return -1;
        }
        int result = entiry[front];
        return result;
    }

    public int Rear() {
        if (isEmpty()) {
            return -1;
        }
        int result = entiry[tail];
        return result;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public boolean isFull() {
        return size == entiry.length;
    }
}
